package com.ruoyi.huanjing.controller;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.huanjing.entity.Atmospheric;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import ucar.ma2.Array;
import ucar.ma2.Index;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
@RestController
public class TReadController {
        /**
     * 读取指定 valid_time 和 pressure_level 下的所有经纬度的 tempData 值
     *
     * @param filePath    NC 文件路径
     * @param timeIndex   时间索引
     * @param pressureIndex 气压索引
     * @return 包含所有经纬度对应的 tempData 值的列表
     * @throws IOException 如果读取文件时发生错误
     */
    @Anonymous
    @GetMapping("/readt")
    private  List<Atmospheric> readTempDataT(String filePath, int timeIndex/*, int pressureIndex*/) throws IOException {

        try (NetcdfFile ncFile = NetcdfFile.open(filePath)) {
            Variable validTimeVar = ncFile.findVariable("depth");
            Variable latVar = ncFile.findVariable("lat");
            Variable lonVar = ncFile.findVariable("lon");
            Variable pressureVar = ncFile.findVariable("pressure_level");
            Variable tempVar = Arrays.stream(new String[]{
                            "temp",
                            "salt"})
                    .map(ncFile::findVariable)
                    .filter(Objects::nonNull)
                    .findFirst()
                    .orElse(null);

            Array latData = latVar.read();
            Array lonData = lonVar.read();
            Array tempData = tempVar.read();

            List<Atmospheric> tempDataList = new ArrayList<>();
            Index index = tempData.getIndex();
            // 初始化变量来记录最大值和最小值
            double minLat = Double.MAX_VALUE;
            double maxLat = Double.MIN_VALUE;
            double minLon = Double.MAX_VALUE;
            double maxLon = Double.MIN_VALUE;
            for (int la = 0; la < latData.getSize(); la++) {
                for (int lo = 0; lo < lonData.getSize(); lo++) {
                    double temp = tempData.getDouble(index.set(timeIndex, /*pressureIndex,*/ la, lo));
                    if (!Double.isNaN(temp)) { // 排除无效值
                        double currentLat = latData.getDouble(la);
                        double currentLon = lonData.getDouble(lo);
                        
                        // 更新最大值和最小值
                        minLat = Math.min(minLat, currentLat);
                        maxLat = Math.max(maxLat, currentLat);
                        minLon = Math.min(minLon, currentLon);
                        maxLon = Math.max(maxLon, currentLon);

                        Atmospheric atmospheric = new Atmospheric()
//                                .setHeight(BigDecimal.valueOf(pressureIndex))
                                .setLon(BigDecimal.valueOf(currentLon))
                                .setLat(BigDecimal.valueOf(currentLat))
                                .setValue(BigDecimal.valueOf(temp))
                                .setTime(BigDecimal.valueOf(validTimeVar.read().getDouble(timeIndex)));

                        tempDataList.add(atmospheric);
                    }
                }
            }
            // 打印最大值和最小值
            System.out.println("Latitude Min: " + minLat + ", Max: " + maxLat);
            System.out.println("Longitude Min: " + minLon + ", Max: " + maxLon);
            return tempDataList;
        } catch (IOException e) {
            throw e;
        }
    }
}
